import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:keyboard_avoider/keyboard_avoider.dart';

import 'package:vegeshop/home.dart';

class LoginPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new NewPageState();
  }
}

class NewPageState extends State<StatefulWidget> {
  final _userName = TextEditingController(); //用户名
  final _userPwd = TextEditingController(); //密码
  var _isLogin = 0;
  final GlobalKey<FormState> _globalKey =
      new GlobalKey<FormState>(); //用于检查输入框是否为空

   _login() {
     if (_userName.text == "admin" && _userPwd.text == "123456") {            
            _isLogin = 1; } else {
        showDialog(
        context: context,
        builder: (context) {
          print('user:${_userName.text}');
          print('pwd:${_userPwd.text}');         
            String err = "登录失败 \n 账号或密码错误";
            return AlertDialog(
              content: Text(err),
            );
            });
    
          }
        
    return _isLogin;
  }

  void _saveLoginMsg() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    preferences.setString("name", _userName.text);
    preferences.setString("pwd", _userPwd.text);
  }

  // 读取账号密码，并将值直接赋给账号框和密码框
  void _getLoginMsg() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    _userName.text = preferences.get("name");
    _userPwd.text = preferences.get("pwd");
  }

  @override
  void initState() {
    super.initState();
    _getLoginMsg();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      //resizeToAvoidBottomInset: false,
        appBar: new AppBar(
          title: new Text(
            "BSTrobot",
          ),
          backgroundColor: Colors.orange,
        ),
        body: KeyboardAvoider(
          autoScroll: true,
            child: Column(                            
           children: <Widget>[   
             new Divider(
            height: 0,
          ),         
          new Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
            new Text('百事通测试组欢迎您！',
                style: TextStyle(
                  fontSize: 20,
                  color: Colors.blue,
                ))
          ]),

          Form(
            key: _globalKey,
            autovalidateMode: AutovalidateMode.always,
            child: Column(
              children: <Widget>[
                TextFormField(
                  controller: _userName,
                  decoration: InputDecoration(
                      labelText: "账号",
                      hintText: "输入你的账号",
                      icon: Icon(Icons.person)),
                  validator: (v) {
                    return v.trim().length > 0 ? null : "用户名不能为空";
                  },
                ),
                TextFormField(
                  controller: _userPwd,
                  decoration: InputDecoration(
                    labelText: "密码",
                    hintText: "输入你的密码",
                    icon: Icon(Icons.lock),
                  ),
                  validator: (v) {
                    return v.trim().length > 5 ? null : "密码不低于6位";
                  },
                  obscureText: true,
                ),
                Padding(
                  padding: EdgeInsets.only(top: 20.0),
                ),
                SizedBox(
                  width: 120.0,
                  height: 50.0,
                  child: RaisedButton(
                    onPressed: () {
                      if (_globalKey.currentState.validate()) {
                        _globalKey.currentState.save();
                        var _tmp = _login();
                        if (_tmp == 1) {
                          Navigator.push(
                      context,
                      new MaterialPageRoute(
                          builder: (context) => new MyHomePage()));
                        }
                        _saveLoginMsg();
                      }
                    },
                    child: Text(
                      "登录",
                      style: TextStyle(color: Colors.white), //字体白色
                    ),
                    color: Colors.blue,
                  ),
                ),
              ],
            ),
          )])
    ));
  }
}
